package com.hisan.freeride.home.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.view.View;
import android.widget.CompoundButton;

import com.blankj.utilcode.util.LogUtils;

import com.hisan.freeride.R;
import com.hisan.freeride.common.base.AppConfig;
import com.hisan.freeride.common.base.BaseActivity;
import com.hisan.freeride.common.callback.DialogCallback;
import com.hisan.freeride.common.listener.IEditTextChangeListener;
import com.hisan.freeride.common.utils.RegexUtils;
import com.hisan.freeride.common.utils.WorksSizeCheckUtil;
import com.hisan.freeride.databinding.BuyaddressBinding;
import com.lljjcoder.citypickerview.widget.CityPicker;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.HttpParams;



/**
 *添加地址页面
 * Created by admin on 2017/5/6.
 */

public class BuyAddressActivity extends BaseActivity<BuyaddressBinding> implements CompoundButton.OnCheckedChangeListener {
    //收货人名称
    private String username;
    //手机号
    private String phone;
    //省
    private String province;
    //市
    private String city;
    //区
    private String district;
    //小区
    private String street;
    //编码
    private String code;
    //是否设置为默认地址
    private int is_default=0;
    //选择的地址id
    private int id;
    private  CityPicker cityPicker;



    @Override
    protected int getLayoutId() {
        return R.layout.buyaddress;
    }

    @Override
    protected void initView() {
        mBinding.add.setEnabled(false);
        //1.创建工具类对象 把要改变颜色的Button先传过去
        WorksSizeCheckUtil.textChangeListener textChangeListener = new WorksSizeCheckUtil.textChangeListener(mBinding.add);
        //2.把所有要监听的edittext都添加进去
        textChangeListener.addAllEditText(mBinding.username,mBinding.phone,mBinding.district);
        //3.接口回调 在这里拿到boolean变量 根据isHasContent的值决定 tv 应该设置什么颜色
        WorksSizeCheckUtil.setChangeListener(new IEditTextChangeListener() {
            @Override
            public void textChange(boolean isHasContent) {
                if(isHasContent){
                    mBinding.add.setEnabled(true);
                }else{
                    mBinding.add.setEnabled(false);
                }
            }
            @Override
            public void afterChange(Editable editable) {
            }
        });
    }

    @Override
    protected void initEvent() {
        mBinding.topBack.setOnClickListener(v -> finishActivity(true));

        cityPicker = new CityPicker.Builder(BuyAddressActivity.this)
                .textSize(14)
                .title("地址选择")
                .titleBackgroundColor("#FFFFFF")
                .confirTextColor("#696969")
                .cancelTextColor("#696969")
                .province("浙江省")
                .city("嘉兴市")
                .district("南湖区")
                .textColor(Color.parseColor("#000000"))
                .provinceCyclic(true)
                .cityCyclic(false)
                .districtCyclic(false)
                .visibleItemsCount(7)
                .itemPadding(10)
                .onlyShowProvinceAndCity(false)
                .build();
        //监听方法，获取选择结果
        cityPicker.setOnCityItemClickListener(citySelected -> {
            //省份
            province = citySelected[0];
            //城市
            city = citySelected[1];
            //区县（如果设定了两级联动，那么该项返回空）
            district = citySelected[2];
            mBinding.city.setText(province+"-"+city+"-"+district);
            //邮编
            code = citySelected[3];
            mBinding.code.setText(code);
        });

        mBinding.selectCity.setOnClickListener(v -> cityPicker.show());
        mBinding.switchCompat.setOnCheckedChangeListener(this);
        mBinding.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mBinding.add.getText().toString().equals("添加收货地址")){
                    username= mBinding.username.getText().toString();
                    phone=mBinding.phone.getText().toString();
                    street=mBinding.district.getText().toString();
                    code=mBinding.code.getText().toString();
                    if (username.length()<1||phone.length()<1||province.length()<1||city.length()
                            <1||district.length()<1||code.length()<1||street.length()<1){
                        showToast("请输入完整信息.....");
                        return;
                    }
                    if (!RegexUtils.isTel(phone)){

                        HttpParams params=new HttpParams();
                        params.put("name",username);
                        params.put("mobile",phone);
                        params.put("province",province);
                        params.put("city",city);
                        params.put("area",district);
                        params.put("postcode",code);
                        params.put("street",street);
                        params.put("is_default",is_default);
                        OkGo.post(AppConfig.SavereceiptAddress)
                                .tag(this)
                                .params(params)
                                .execute(new DialogCallback<Object>(BuyAddressActivity.this){
                                    @Override
                                    public void onSuccess(com.lzy.okgo.model.Response<Object> response) {
                                        super.onSuccess(response);
                                        if (isOk(response)){
                                            showToast("添加成功");
                                            Intent intent=new Intent();
                                            setResult(99, intent);
                                            finishActivity(true);
                                        }else {
                                            showError(response);
                                        }
                                    }
                                    @Override
                                    public void onError(com.lzy.okgo.model.Response<Object> response) {
                                        super.onError(response);
                                        showError(response);
                                    }
                                });
                    }else{
                        showToast("请输入正确的手机号");
                    }
                }else{
                    username= mBinding.username.getText().toString();
                    phone=mBinding.phone.getText().toString();
                    street=mBinding.district.getText().toString();
                    code=mBinding.code.getText().toString();
                    if (username.length()<1||phone.length()<1||province.length()<1||city.length()
                            <1||district.length()<1||code.length()<1||street.length()<1){
                        showToast("请输入完整信息.....");
                        return;
                    }
                    if (!RegexUtils.isTel(phone)){
                        OkGo.post(AppConfig.SavereceiptAddress)
                                .tag(this)
                                .params("id",id+"")
                                .params("name",username)
                                .params("mobile",phone)
                                .params("province",province)
                                .params("city",city)
                                .params("area",district).
                                params("postcode",code).
                                params("street",street)
                                .params("is_default",is_default+"")
                                .execute(new DialogCallback<Object>(BuyAddressActivity.this) {
                                    @Override
                                    public void onSuccess(com.lzy.okgo.model.Response<Object> response) {
                                        super.onSuccess(response);
                                        if (isOk(response)){
                                            showToast("修改成功");
                                            Intent intent=new Intent();
                                            setResult(99, intent);
                                            finishActivity(true);
                                        }else {
                                            showError(response);
                                        }
                                    }
                                    @Override
                                    public void onError(com.lzy.okgo.model.Response<Object> response) {
                                        super.onError(response);
                                        showError(response);
                                    }
                                });
                    }else {
                        showToast("请输入正确的手机号");
                    }
                }
            }
        });
    }

    @Override
    protected void loadData(boolean isRefresh) {
        Bundle bundle=getIntent().getExtras();
        if (bundle!=null){
            mBinding.add.setText("编辑收货地址");
            id=bundle.getInt("id");
            province=bundle.getString("province");
            city=bundle.getString("city");
            district=bundle.getString("area");
            String name=bundle.getString("name");
            String mobile=bundle.getString("mobile");
            String street=bundle.getString("street");
            String code=bundle.getString("code");
            int isdefault=bundle.getInt("default");
            mBinding.city.setText(province+"-"+city+"-"+district);
            mBinding.username.setText(name);
            mBinding.phone.setText(mobile);
            mBinding.district.setText(street);
            mBinding.code.setText(code);
            if (isdefault==1){
                mBinding.switchCompat.setChecked(true);
            }else{
                mBinding.switchCompat.setChecked(false);
            }
        }else{
            mBinding.add.setText("添加收货地址");
        }
    }


    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.switch_compat:
                if (isChecked) {
                    is_default=1;
                } else {
                    is_default=0;
                }
                break;
        }
    }
}
